// 画面点位数据
let pointArr = []
// 当前点位的数据
let nowPoint = []
// 当前多边形中的点和线实体集合
let pointAndLineEntity = {
  pointEntityArr: [],
  lineEntityArr: [],
  demoLineEntityArr: [],
  polygonEntityArr: [],
}
// 当前选择的编辑点
let nowEditPoint = []
// 是否开始画多边形
let isDrawLine = false
// 是否形成面
let isDrawPolyGon = false
// 事件
const editCircleFunC  = {
  // 删除实体
  // 删除鼠标移动生成的线
  delDemoEntity: (drawArr) => {
    drawArr.forEach((item) => {
      viewer.entities.remove(item)
    })
    drawArr=[]
  },
  // 画圆心点
  drawPoint: (lonAndLat) => {
    const entityPoint = viewer.entities.add({
      position: Cesium.Cartesian3.fromDegrees(lonAndLat[0], lonAndLat[1], lonAndLat[2]),
      name: 'circle_center',
      point: {
        color: Cesium.Color.fromCssColorString('#ffffff'), //点的颜色
        outlineColor: Cesium.Color.fromCssColorString('#0091ff'), //边框轮廓的颜色
        pixelSize: 8, //点的像素大小
        outlineWidth:2 //边框轮廓的宽度
      },
    })
    return entityPoint
  },
  //画拖动半径的点
  drawDragPoint: (lonAndLat) => {
    const entityPoint = viewer.entities.add({
      position: Cesium.Cartesian3.fromDegrees(lonAndLat[0], lonAndLat[1], lonAndLat[2]),
      name: 'circle_drag',
      point: {
        color: Cesium.Color.fromCssColorString('#0091ff'), //点的颜色
        outlineColor: Cesium.Color.fromCssColorString('#ffffff'), //边框轮廓的颜色
        pixelSize: 8, //点的像素大小
        outlineWidth:2 //边框轮廓的宽度
      },
    })
    return entityPoint
  },
  //根据圆心，半径，角度计算圆边缘的某个点坐标
  getLonAndLat:(lng, lat, brng, dist)=> {
    function rad(d) {
      return (d * Math.PI) / 180.0;
    }

    function deg(x) {
      return (x * 180) / Math.PI;
    }
    //大地坐标系资料WGS-84 长半径a=6378137 短半径b=6356752.3142 扁率f=1/298.2572236
    var a = 6378137;
    var b = 6356752.3142;
    var f = 1 / 298.257223563;

    var lon1 = lng * 1;
    var lat1 = lat * 1;
    var s = dist;
    var alpha1 = rad(brng);
    var sinAlpha1 = Math.sin(alpha1);
    var cosAlpha1 = Math.cos(alpha1);

    var tanU1 = (1 - f) * Math.tan(rad(lat1));
    var cosU1 = 1 / Math.sqrt(1 + tanU1 * tanU1),
      sinU1 = tanU1 * cosU1;
    var sigma1 = Math.atan2(tanU1, cosAlpha1);
    var sinAlpha = cosU1 * sinAlpha1;
    var cosSqAlpha = 1 - sinAlpha * sinAlpha;
    var uSq = (cosSqAlpha * (a * a - b * b)) / (b * b);
    var A = 1 + (uSq / 16384) * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
    var B = (uSq / 1024) * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));

    var sigma = s / (b * A),
      sigmaP = 2 * Math.PI;
    while (Math.abs(sigma - sigmaP) > 1e-12) {
      var cos2SigmaM = Math.cos(2 * sigma1 + sigma);
      var sinSigma = Math.sin(sigma);
      var cosSigma = Math.cos(sigma);
      var deltaSigma =
        B *
        sinSigma *
        (cos2SigmaM +
          (B / 4) *
            (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) -
              (B / 6) * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
      sigmaP = sigma;
      sigma = s / (b * A) + deltaSigma;
    }

    var tmp = sinU1 * sinSigma - cosU1 * cosSigma * cosAlpha1;
    var lat2 = Math.atan2(sinU1 * cosSigma + cosU1 * sinSigma * cosAlpha1, (1 - f) * Math.sqrt(sinAlpha * sinAlpha + tmp * tmp));
    var lambda = Math.atan2(sinSigma * sinAlpha1, cosU1 * cosSigma - sinU1 * sinSigma * cosAlpha1);
    var C = (f / 16) * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
    var L = lambda - (1 - C) * f * sinAlpha * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));

    var revAz = Math.atan2(sinAlpha, -tmp); // final bearing

    var lngLatObj = { lng: lon1 + deg(L), lat: deg(lat2) };
    return lngLatObj;
  },
  //根据两点坐标计算距离
  getPointsDistance:(start_point, end_point)=> {
    // 经纬度转换为世界坐标
    var start_position = Cesium.Cartesian3.fromDegrees(...start_point);
    var end_position = Cesium.Cartesian3.fromDegrees(...end_point);
    // 返回两个坐标的距离（单位：米）
    return Cesium.Cartesian3.distance(start_position, end_position);
  },

  // 画圆
  drawCircle: (id,lonAndLat,radius,color,zIndex) => {
    const entityCircle=viewer.entities.add({
      id:id,
      name: 'circle_name',
      position: Cesium.Cartesian3.fromDegrees(...lonAndLat),
      // ellipse：椭圆
      ellipse: {
        semiMinorAxis: radius, //半短轴的大小
        semiMajorAxis: radius, //半长轴的大小
        fill:true,
        material: Cesium.Color.fromCssColorString(color).withAlpha(0.4),
        outline: true, //是否有边框,当fill为true,边框不显示
        outlineColor:Cesium.Color.fromCssColorString('#4d90fe'), //边框轮廓的颜色
        outlineWidth:1, //边框的大小，目前无法更改宽度大小
        zIndex:zIndex,
      },
    })
    return entityCircle;
  },
  // 经纬度转换
  getLonOrLat: (cartesian2) => {
    var cartesian = viewer.scene.globe.pick(viewer.camera.getPickRay(cartesian2), viewer.scene)
    let cartorgraphic = Cesium.Cartographic.fromCartesian(cartesian)
    let lon = Cesium.Math.toDegrees(cartorgraphic.longitude) // 经度
    let lat = Cesium.Math.toDegrees(cartorgraphic.latitude) // 纬度
    return {
      longitude: lon,
      latitude: lat,
    }
  },

  // 重置
  klk: () => {
    editPolyGonFunC.delDemoEntity('point_name')
    isDrawLine = false
    isDrawPolyGon = false
    pointAndLineEntity = {
      pointEntityArr: [],
      lineEntityArr: [],
      demoLineEntityArr: [],
      polygonEntityArr: [],
    }
    nowEditPoint = []
  },
}
export default editCircleFunC
